<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/5/28
 * Time: 下午6:33
 */

namespace EasySwoole\EasySwoole;


use App\Lib\Process\ConsumerTest;
use EasySwoole\Component\Di;
use App\Lib\Redis\Redis;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use EasySwoole\Utility\File;
use EasySwoole\Http\Message\Status;
use EasySwoole\EasySwoole\ServerManager;
use App\Producer\Process as ProducerProcess;
use App\Consumer\Process as ConsumerProcess;

class EasySwooleEvent implements Event
{

    public static function initialize()
    {
        // TODO: Implement initialize() method.
        date_default_timezone_set('Asia/Shanghai');
        self::loadConf();
    }
    /**
     * 加载配置文件
     */
    public static function loadConf()
    {
        //遍历目录中的文件
        $files = File::scanDirectory(EASYSWOOLE_ROOT . '/Config');
        if (is_array($files)) {
            //$files['files'] 一级目录下所有的文件,不包括文件夹
            foreach ($files['files'] as $file) {
                $fileNameArr = explode('.', $file);
                $fileSuffix = end($fileNameArr);
                if ($fileSuffix == 'php') {
                    \EasySwoole\EasySwoole\Config::getInstance()->loadFile($file);//引入之后,文件名自动转为小写,成为配置的key
                }
            }
        }
    }
    public static function mainServerCreate(EventRegister $register)
    {
        // TODO: Implement mainServerCreate() method.
        //mysql相关
        // 之前es2 - mysql 需要协程链接池 去优化
        Di::getInstance()->set('MYSQL',\MysqliDb::class,Array (
                'host' => '127.0.0.1',
                'user' => 'root',
                'password' => '123456',
                'db'=> 'video',
                'port' => 3306,
                'charset' => 'utf8')
        );
        //redis
        Di::getInstance()->set('REDIS', Redis::getInstance());
        // TODO: Implement mainServerCreate() method.
        $allNum = 3;
        for ($i = 0 ;$i < $allNum;$i++){
//            ServerManager::getInstance()->getSwooleServer()->addProcess("imooc_consumer_testp{$i}",ConsumerTest::class);
            ServerManager::getInstance()->getSwooleServer()->addProcess((new ConsumerTest("imooc_consumer_testp_{$i}"))->getProcess());
        }
        // 生产者
//        ServerManager::getInstance()->getSwooleServer()->addProcess((new ProducerProcess())->getProcess());
//        // 消费者
//        ServerManager::getInstance()->getSwooleServer()->addProcess((new ConsumerProcess())->getProcess());
    }

    public static function onRequest(Request $request, Response $response): bool
    {
        // TODO: Implement onRequest() method.
        $response->withHeader('Access-Control-Allow-Origin', '*');
        $response->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
        $response->withHeader('Access-Control-Allow-Credentials', 'true');
        $response->withHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
        if ($request->getMethod() === 'OPTIONS') {
            $response->withStatus(Status::CODE_OK);
            return false;
        }
        return true;
    }

    public static function afterRequest(Request $request, Response $response): void
    {
        // TODO: Implement afterAction() method.
    }
}